<!-- 按钮一键登录 -->
<template>
	<view>
		<view class="login" v-if="!loading">
			<u-navbar :background="'rgba(255,99,14,' + 0 + ')'" back-icon-color="#fff" :border-bottom='false'></u-navbar>
			<view class="u-fc box">
				<view class="logo-img u-fc u-f-ajc">
					<image class="imgs" src="https://sbb-sbb.oss-cn-shenzhen.aliyuncs.com/Open_source_project/auth/mall-logo.png" mode="heightFix"></image>
					<view class="login-box u-fc u-f-jsb">
						<view class="agree u-f">登录即代表同意OPEN-MALL<navigator hover-class="none" class="text-blue" url='/pages/auth/privacy/privacy'>《法律声明及隐私政策》</navigator></view>
						<button class="loginWeChatBtn wx-btn u-f-ac" open-type="getPhoneNumber" @getphonenumber="wechatLogin">
							<image src="https://sbb-sbb.oss-cn-shenzhen.aliyuncs.com/Open_source_project/auth/wxLogin.png"></image><text>微信快捷登录</text>
						</button>
					</view>
				</view>
				
			</view>
		</view>
		<our-loading  :active='loading' :translateY='90' :isFullScreen='true' />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				res: {},
				code:'',
				loading: true,
				levelId: ''
			}
		},
		onLoad(option) {
			this.levelId = option.levelId;
			if(option.scene){
				this.levelId = decodeURIComponent(option.scene)
			}
			let that = this;
			uni.login({
				provider: 'weixin',
				success({ errMsg, code }) {
					if (errMsg == 'login:ok') that.code = code
				}
			});
		},
		onShow() {
			// 判断是否授权
			this.res = uni.getStorageSync('wechatAuth') || '';
			if(this.res){
				this.loading = false
			}else{
				this.$u.route({url:'/pages/auth/btnAuth/wechatAuth',type:'redirect'})
			}
		},
		methods: {
			// 小程序登录
			wechatLogin(e) {
				var _this = this;
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					wx.checkSession({
					     success(res) {
							let formdata = {
								code: _this.code,
								iv: e.detail.iv,
								encryptedData: e.detail.encryptedData,
								avatarUrl: _this.res.userInfo.avatarUrl,
								gender: _this.res.userInfo.gender,
								nickname: _this.res.userInfo.nickName,
								levelId: _this.levelId
							};
							// 弹出正在登录的弹框，登录请求
							uni.showLoading({ title: '正在登录' });
							//请求后台登录
							_this.$http.post('/api/business/appletLogin', formdata, { header: { authType: 'false' } }).then(res => {
								uni.hideLoading();
								if(!res.success) return _this.$tip.toast(res.msg);
								//添加levelId
								res.data.levelId = _this.levelId ? _this.levelId : null;
								_this.$store.commit('getUserInfo', res.data);
								res.data.examineStatus == 1 ? uni.switchTab({url:'../../home/home'}) : _this.login(res) 
							});
						},fail(err) {
						// session_key 已经失效，需要重新执行登录流程
						uni.login({
							provider: 'weixin',
							success({ errMsg, code }) {
								if (errMsg == 'login:ok') {
									that.code = code
								}
							}
						});
						}
					})
				} else {
					this.$u.route({ type: 'navigateBack'});
				}
			},
			login(res){
				this.$tip.toast(res.msg);
				setTimeout( ()=>{this.$u.route({url:'/pages/auth/apply/apply',type:'reLaunch'})},1500)
			}
		}
	}
</script>
<style>
	page {
		background-color: #fff;
	}
</style>
<style scoped lang="scss">
.login{
	position: relative;
	height: 100vh;
	background: url(https://sbb-sbb.oss-cn-shenzhen.aliyuncs.com/Open_source_project/auth/loginBg.jpg);
	background-repeat: no-repeat;
	background-position: center;
	background-size: cover;
	overflow: hidden;
}
.box{
	justify-content: space-between;
	height: 100%;
	.logo-img{
		flex: 1;
	}
	.logo-img .imgs{
		display: block;
		height: 266rpx;
		margin: 0 auto;
	}
}
.login-box{
	background-color: rgba(#fff,0.8);
	margin: 0 20rpx;
	border-radius: 28rpx;
	padding: 48rpx 28rpx;
	margin-top: 80rpx;
	.agree{
		font-size: 26rpx;
	}
}
.loginWeChatBtn {
	width: 100%;
	height: 90rpx;
	font-size: 30rpx;
	border: none;
	color: #fff;
	margin-bottom: 40rpx;
	margin-top: 40rpx;
	border-radius: 60rpx;
	justify-content: center;
	box-shadow: 0 6rpx 20rpx rgba(69,171,46,0.5);
	image{
		width: 48rpx;
		height: 40rpx;
		margin-right: 10rpx;
	}
}
.wx-btn{
	background-color: rgb(69,171,46);
}
</style>

